﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace PhotoGallery.Application.HttpModule
{
    public class ErrorHttpModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(OnError);
        }

        public void OnError(object obj, EventArgs args)
        {
            HttpContext ctx = HttpContext.Current;
            HttpResponse response = ctx.Response;
            HttpRequest request = ctx.Request;
            Exception exception = ctx.Server.GetLastError();
            response.Write("系统发生一个问题， " +
                           "Please press the back button on" +
                           " your browser and try again.<br/>");
            response.Write("If the problem persists, please " +
                           "contact technical support<p/>");
            response.Write("Information below is for " +
                           "technical support:<p/>");

            string errorInfo = "<p/>URL: " + ctx.Request.Url.ToString();
            errorInfo += "<p/>Stacktrace:---<br/>" +
               exception.InnerException.StackTrace.ToString();
            errorInfo += "<p/>Error Message:<br/>" +
               exception.InnerException.Message;

            //Write out the query string 

            response.Write("Querystring:<p/>");

            for (int i = 0; i < request.QueryString.Count; i++)
            {
                response.Write("<br/>" +
                     request.QueryString.Keys[i].ToString() + " :--" +
                     request.QueryString[i].ToString() + "--<br/>");// + nvc.

            }

            //Write out the form collection

            response.Write("<p>---------------" +
                           "----------<p/>Form:<p/>");

            for (int i = 0; i < request.Form.Count; i++)
            {
                response.Write("<br/>" +
                         request.Form.Keys[i].ToString() +
                         " :--" + request.Form[i].ToString() +
                         "--<br/>");// + nvc.

            }

            response.Write("<p>-----------------" +
                           "--------<p/>ErrorInfo:<p/>");

            response.Write(errorInfo);

            // --------------------------------------------------

            // To let the page finish running we clear the error

            // --------------------------------------------------


            ctx.Server.ClearError();
        }

       

    }
}
